<%@page import="com.oval.foundation.metadata.pageflow.*"%>
<%@page import="com.oval.foundation.metadata.engine.*"%>
<%@page import="com.oval.foundation.metadata.workflow.*"%>
<%@page import="com.oval.foundation.container.*"%>
<%@page import="com.oval.foundation.view.*"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>

<html>
	<head>
		<title>Invoked Statistic</title>
		
	</head>
	<body>
		<%
		String type=request.getParameter("invokedtype");
		if(type==null)
			type="";
		%>
		<form name="frm">
			<select name="invokedtype" onchanged="invokedTypeChanged()">
				<option value="" <%=type.equals("")?"selected":""%>>---</option>
				<option value="vt"  <%=type.equals("vt")?"selected":""%>>VirtualTable</option>
				<option value="ct" <%=type.equals("ct")?"selected":""%>>CoreTable</option>
				<option value="job" <%=type.equals("job")?"selected":""%>>Job</option>
				<option value="jobunit" <%=type.equals("jobunit")?"selected":""%>>JobUnit</option>
				<option value="component" <%=type.equals("component")?"selected":""%>>Component</option>
				<option value="page" <%=type.equals("page")?"selected":""%>>Page</option>
				<option value="jsp" <%=type.equals("jsp")?"selected":""%>>Jsp/Image/Servlet</option>
			</select>
			<input type="submit" value="GO"/>
		</form>
		<table border="1">
			<tr>
				<td>No.</td>
				<td>Name</td>
				<td>URL</td>
				<td>Count</td>
		  </tr>
		
	  <%
	    Iterator it=null;
		  HashMap jspMap=null;
	  if(type.equals("vt")){
	  	it=VTableContainer.getInvokedCount();
	  }
		else  if(type.equals("ct")){
	  	it=CoreTableContainer.getInvokedCount();
	  }
	  else  if(type.equals("job")){
	  	it=JobContainer.getInvokedCount();
	  }
	   else  if(type.equals("jobunit")){
	  	it=JobUnitContainer.getInvokedCount();
	  }
	  else  if(type.equals("component")){
	  	it=ComponentContainer.getInvokedCount();
	  }
	  else  if(type.equals("page")){
	  	it=PageContainer.getInvokedCount();
	  }
	  else  if(type.equals("jsp")){
	  	jspMap=FileStatisticFilter.getInvokedFileStatistic();
	  	if(jspMap!=null)
	  		it=jspMap.keySet().iterator();
	  }
	  
	  if(it!=null){
	  int num=0;
	  String name=null;
	  String uri=null;
	  Long count=null;
	  Object obj=null;
	  File fileSource=null;
	  FileInputStream inStream=null;
	   FileOutputStream outStream=null;
	  String sTarget=null;
	  File targetFile=null;
	  byte[] buff=new byte[1024];
	  while(it.hasNext()){
		name=(String)it.next();
		if(type.equals("vt")){
	  	obj =VTableContainer.findVTableMeta(name);
	  	count=VTableContainer.getInvokedCount(name);
	  	if(obj!=null && ((OvalVTable)obj).getVTableFile()!=null)
	  		uri=((OvalVTable)obj).getVTableFile().getAbsolutePath();
	  	else
	  		uri="";
	  	if(uri!=null && uri.trim().length()>0 && uri.toLowerCase().endsWith(".xml"))
	  	{
	  		fileSource=new File(uri);
	  		sTarget=uri.substring(uri.indexOf("oval.war\\WEB-INF\\config\\")+"oval.war\\WEB-INF\\config\\".length());
	  	}	
	  	 else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  }
		else  if(type.equals("ct")){
			obj =CoreTableContainer.findCoreTable(name);
	  	count=CoreTableContainer.getInvokedCount(name);
	  	if(obj!=null && ((OvalCoreTable)obj).getCoreTableFile()!=null)
	  		uri=((OvalCoreTable)obj).getCoreTableFile().getAbsolutePath();
	  	else
	  		uri="";
	  		if(uri!=null && uri.trim().length()>0 && uri.toLowerCase().endsWith(".xml"))
	  	{
	  		fileSource=new File(uri);
	  		sTarget=uri.substring(uri.indexOf("oval.war\\WEB-INF\\config\\")+"oval.war\\WEB-INF\\config\\".length());
	  	}	
	  	 else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  	
	  }
	  else  if(type.equals("job")){
	  	obj =JobContainer.findJobMeta(name);
	  	count=JobContainer.getInvokedCount(name);
	  	if(obj!=null && ((Entity)obj).getEntityFile()!=null)
	  		uri=((Entity)obj).getEntityFile().getAbsolutePath();
	  	else
	  		uri="";
	  		if(uri!=null && uri.trim().length()>0 && uri.toLowerCase().endsWith(".xml"))
	  	{
	  		fileSource=new File(uri);
	  		sTarget=uri.substring(uri.indexOf("oval.war\\WEB-INF\\config\\")+"oval.war\\WEB-INF\\config\\".length());
	  	}	
	  	 else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  	
	  }
	   else  if(type.equals("jobunit")){
	   	obj =JobUnitContainer.findJobUnitMeta(name);
	  	count=JobUnitContainer.getInvokedCount(name);
	  	if(obj!=null && ((Entity)obj).getEntityFile()!=null)
	  		uri=((Entity)obj).getEntityFile().getAbsolutePath();
	  	else
	  		uri="";
	  		if(uri!=null && uri.trim().length()>0 && uri.toLowerCase().endsWith(".xml"))
	  	{
	  		fileSource=new File(uri);
	  		sTarget=uri.substring(uri.indexOf("oval.war\\WEB-INF\\config\\")+"oval.war\\WEB-INF\\config\\".length());
	  	}	
	  	 else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  	
	  }
	  else  if(type.equals("component")){
	  		obj =ComponentContainer.findComponentMeta(name);
	  	count=ComponentContainer.getInvokedCount(name);
	  	if(obj!=null && ((Entity)obj).getEntityFile()!=null)
	  		uri=((Entity)obj).getEntityFile().getAbsolutePath();
	  	else
	  		uri="";
	  		if(uri!=null && uri.trim().length()>0 && uri.toLowerCase().endsWith(".xml"))
	  	{
	  		fileSource=new File(uri);
	  		sTarget=uri.substring(uri.indexOf("oval.war\\WEB-INF\\config\\")+"oval.war\\WEB-INF\\config\\".length());
	  	}	
	   else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  	
	  }
	  else  if(type.equals("page")){
	  	obj =PageContainer.getPage(name);
	  	count=PageContainer.getInvokedCount(name);
	  	if(obj!=null && ((OvalPage)obj).getPageFile()!=null)
	  		uri=((OvalPage)obj).getPageFile().getAbsolutePath();
	  	else
	  		uri="";
	  		if(uri!=null && uri.trim().length()>0 && uri.toLowerCase().endsWith(".xml"))
	  	{
	  		fileSource=new File(uri);
	  		sTarget=uri.substring(uri.indexOf("oval.war\\WEB-INF\\config\\")+"oval.war\\WEB-INF\\config\\".length());
	  	}	
	   else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  	
	  }
	  else  if(type.equals("jsp")){
	  	uri=name;
	  	count=(Long)jspMap.get(name);
	  	if(uri!=null && uri.trim().length()>0 && uri.startsWith("/oval/"))
	  	{
	  	  String tmp=uri.substring("/oval/".length());
	  	 // System.out.println(tmp);
	  	  if(tmp!=null && tmp.trim().length()>0)
	  	  {
	  	  	fileSource=new File("F:/jboss-4.0.5.GA/server/dcms40/deploy/oval.war/",tmp);
	  	  	sTarget="jsp/"+tmp;
	  	  	}
	  	   else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  	}
	   else
	  	 	{
	  	fileSource=null;
	  	sTarget=null;
	  	}
	  }
	  if(fileSource!=null && fileSource.exists() && fileSource.isFile() && sTarget!=null && sTarget.trim().length()>0)
	  {
	  	//System.out.println(sTarget);
	  	targetFile=new File("F:/temp/oval-base/",sTarget);
	  	
	  	if(!targetFile.exists() || !targetFile.isFile())
	  	{
	  		 boolean flag=targetFile.getParentFile().mkdirs();
	  		 
	  		 	 try
	  		 	 {
	  		 	 
	  		 	 	inStream=new FileInputStream(fileSource);
	  		 	 	outStream=new FileOutputStream(targetFile);
	  		 	 
	  		 	 	int len=inStream.read(buff);
	  		 	 	while(len>0)
	  		 	 	{
	  		 	 		outStream.write(buff,0,len);
	  		 	 		len=inStream.read(buff);
	  		 	 	}
	  		 	 	
	  		 	 }
	  		 	 catch(Exception ex)
	  		 	 {
	  		 	  System.out.println(ex+"["+fileSource+"]");
	  		 	  
	  		 	 }
	  		 	 finally
	  		 	 {
	  		 	 	try{inStream.close();inStream=null;}catch(Exception ex){}
	  		 	 	try{outStream.close();outStream.close();}catch(Exception ex){}
	  		 	 	
	  		 	 }
	  		 
	  	}
	  }
	  %>
	  <tr>
	  	<td><%=(++num)%></td>
	  	<td><%=name%></td>
	  	<td><%=uri%></td>
	  	<td><%=count%></td>
	  </tr>
	  <%}}%>
	</table>
	</body>
</html>